package com.ly.jdgoodsdata.common.connection;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONConfig;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ly.jdgoodsdata.entity.CommentsCount;
import com.ly.jdgoodsdata.entity.Shop;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class JDConnection {
    //    获取评论数量url
    private static final String COMMENT_NUM_URL = "https://club.jd.com/comment/productCommentSummaries.action?referenceIds=";
    //    查询url
    public static final String SEARCH_URL = "https://search.jd.com/Search?keyword=";
    //    下一页
    public static final String NEXT_URL = "https://search.jd.com/s_new.php?keyword={keyword}&page={page}&s=26&scrolling=y&tpl=3_M&isList=0&show_items=";
    //    详情url
    public static final String DETAIL_URL = "https://item.jd.com/";

    public static final String PRICE_URL = "https://p.3.cn/prices/mgets?skuIds=";   //多参分隔符 %2C

    /**
     * 爬虫入口，获取初始skuId
     *
     * @param keyword
     */
    public static void search(String keyword) {
        HashSet<Shop> shopSet = new HashSet<>();
        HashSet<Long> skuSet = new HashSet<>();
        Connection connect = Jsoup.connect(SEARCH_URL + keyword);
        try {
            Document document = connect.get();
            Elements goods = document.select("#J_goodsList>.gl-warp>li");
            analyzeSearchDocument(shopSet, skuSet, goods);
            //            获取总页数
            String script = document.select("script").get(3).data();
            Pattern pattern = Pattern.compile("page_count:\"(\\d*)\",");
            Matcher matcher = pattern.matcher(script);
            long totalPage = 0;
            while (matcher.find()) {
                totalPage = Long.valueOf(matcher.group(1));
            }
            //            下一页数据
            for (int page = 2; page <= totalPage; page++) {
                Connection nextConnection = Jsoup.connect(NEXT_URL.replace("{keyword}", keyword).replace("{page}", page + ""));
                nextConnection.header("Referer", SEARCH_URL + keyword);
                Elements nextGoods = nextConnection.get().select("body>li");
                analyzeSearchDocument(shopSet, skuSet, nextGoods);
            }
            shopSet.forEach(System.out::println);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取评价数量
     *
     * @param skuId
     */
    public static void getCommentNum(List skuIdList) {
        Connection connect = Jsoup.connect(COMMENT_NUM_URL + StrUtil.join(",", skuIdList));
        try {
            String json = connect.get().text();
            JSONObject jsonObject = JSONUtil.parseObj(json, getJSONConfig());
            JSONArray jsonArray = (JSONArray) jsonObject.get("CommentsCount");
            List<CommentsCount> commentsCountList = JSONUtil.toList(jsonArray, CommentsCount.class);
            commentsCountList.forEach(System.out::println);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public static void getPrice(Long skuId) {

    }

    /**
     * 获取商品详细信息
     *
     * @param skuId
     */
    public static void getGoodsInfo(Long skuId) {
        try {
            Document document = Jsoup.connect(DETAIL_URL + skuId + ".html").get();
            Element firstLi = document.select(".detail .parameter2 li").first();
            System.out.println(firstLi.text());
            String script = document.selectFirst("head script[charset]").data();
            Pattern pattern = Pattern.compile("colorSize:.*}]|name:.*'|shopId:'.*'");
            Matcher matcher = pattern.matcher(script);
            while (matcher.find()) {
                System.out.println(matcher.group());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static JSONConfig getJSONConfig() {
        JSONConfig jsonConfig = new JSONConfig();
        jsonConfig.setIgnoreCase(true);
        return jsonConfig;
    }

    /**
     * 分析搜索结果页面的数据
     *
     * @param shopSets
     * @param skuSet
     * @param goods
     */
    private static void analyzeSearchDocument(HashSet shopSet, HashSet skuSet, Elements goods) {
        goods.forEach(li -> {
            String skuId = li.attr("data-sku");
            Elements el = li.select(".gl-i-wrap");
            String shopName = el.select(".p-shop a").text();
            String href = el.select(".p-shop a").attr("href");
            //                如果没有href从另一个地方获取
            if (href == null || href.length() <= 0) {
                String onclick = li.select(".p-name a").attr("onclick");
                int start = onclick.indexOf(",");
                int end = onclick.indexOf("',");
                String shopId = onclick.substring(start + 3, end);
                shopSet.add(new Shop(Long.valueOf(shopId),shopName));
            } else {
                int start = href.indexOf("-");
                int end = href.indexOf(".html");
                try {
                    String shopId = href.substring(start + 1, end);
                    shopSet.add(new Shop(Long.valueOf(shopId),shopName));
                } catch (Exception e) {
                    System.out.println(li);
                }
            }
            skuSet.add(Long.valueOf(skuId));
        });
    }
}
